package com.fly.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fly.demo.entity.User;
import com.fly.demo.mapper.UserMapper;
import com.fly.demo.service.IUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author fly
 * @since 2025-04-23
 */

@RestController
@RequestMapping("/user")
public class UserController {

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);


    @Autowired
    IUserService iUserService;

    // http://localhost:8081/user/1
    @GetMapping("/{id}")
    public User queryUserByName(@PathVariable Long id){
        User user= iUserService.getById(id);
        logger.info("userInfo: "+ user.toString());
        return user;
    }

    // http://localhost:8081/user/queryUsers?pageNum=1&pageSize=5
    @GetMapping("/queryUsers")
    public IPage<User> queryUsers(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "10") int pageSize) {

        QueryWrapper<User> queryWrapper = Wrappers.query();
        queryWrapper.eq("username", "fly");

        Page<User> page = new Page<>(pageNum, pageSize);
        IPage<User> userPage= iUserService.page(page,queryWrapper);
        logger.info("userPage: "+ userPage.toString());
        return userPage;
    }
}
